import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import QtMultimedia

Rectangle {
  id: root
  anchors.fill: parent
  color: "#00000000"  // 透明背景

  property int targetPlayerId: 0
  property int beastType: 1
  property var beastMap: ["azure_dragon", "white_tiger", "vermilion_bird", "black_tortoise"]
  property var roomScene: parent.parent
  property int duration: 2000
  property var goPositionX: 0
  property var goPositionY: 0

  // 神兽图片
  Image {
    id: beastImageItem
    source: "../image/effects/sx__" + beastMap[beastType] + ".jpg"
    width: 250
    height: 280

    opacity: 0
    scale: 0.5
    x: parent.width/2 - width/2
    y: parent.height/2 - height/2
    smooth: true
    antialiasing: true
  }

  Image {
    id: start
    source: "../image/mark/@!!" + beastMap[beastType] +  ".png"
    width: 15
    height: 15
    opacity: 0
    scale: 0
    x: parent.width/2 - width/2
    y: parent.height/2 - height/2
    smooth: true
    antialiasing: true
  }


  // 动画序列
  SequentialAnimation {
    id: flyAnimation
    running: false

    // 三级缩放动画
    NumberAnimation {
      target: beastImageItem
      property: "scale"
      from: 0.0
      to: 1.0
      duration: 300
    }

    // 放到最大
    ParallelAnimation {
      NumberAnimation {
        target: beastImageItem
        property: "scale"
        from: 1.0
        to: 1.5
        duration: 200
      }
      NumberAnimation {
        target: beastImageItem
        property: "opacity"
        from: 1
        to: 1
        duration: 200
      }
      NumberAnimation {
        target: beastImageItem
        property: "rotation"
        from: 0
        to: -15
        duration: 200
      }
    }
    
    // 持续展示
    PauseAnimation {
      duration: 300
    }

    // 缩小淡出
    ParallelAnimation {
      NumberAnimation {
        target: beastImageItem
        property: "scale"
        from: 1.5
        to: 0.0
        duration: 400
      }
      NumberAnimation {
        target: beastImageItem
        property: "opacity"
        from: 1
        to: 0.3
        duration: 400
      }
    }
    
    // 星星动画
    ParallelAnimation {
      NumberAnimation {
        target: start
        property: "scale"
        from: 0.0
        to: 1.0
        duration: 100
      }
      NumberAnimation {
        target: start
        property: "opacity"
        from: 0
        to: 1
        duration: 300
      }
      PropertyAnimation {
        target: start
        property: "x"
        to: goPositionX - 8
        duration: 500
        easing.type: Easing.InOutQuad
      }
      PropertyAnimation {
        target: start
        property: "y"
        to: goPositionY
        duration: 500
        easing.type: Easing.InOutQuad
      }
    }
    ParallelAnimation {
      NumberAnimation {
        target: start
        property: "scale"
        from: 1.0
        to: 0.0
        duration: 100
      }
    }
    ScriptAction {
      script: {
        root.finished()
      }
    }
  }
  // 完成回调
  function finished() {
    roomScene.bigAnim.source = "";
  }

  // 加载数据并启动动画
  function loadData(data) {
    root.targetPlayerId = data.playerId
    root.beastType = data.beastType
    const pos = roomScene.getPhoto(root.targetPlayerId)
    goPositionX = pos.x + pos.width
    goPositionY = pos.y + pos.height
    console.log(goPositionX, goPositionY)
  }

   Component.onCompleted: {
    flyAnimation.running = true
  }

}